package com.axonactive.yolo.dto;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;

import com.axonactive.yolo.util.Utils;

/**
 * 
 * Outbound: Luot di
 * 
 */
/**
 * @Attributes numberStops, totalDuration, totalDurationHHMM, airportsExpanded,
 *             carriers, departAirports, arriveAirports, dayOver
 */
public class OutBound {

	String numberStops;
	String totalDuration;
	String totalDurationHHMM;
	String airportsExpanded;
	String carriers = "";
	List<AirportDeparture> departAirports;
	List<AirportArrival> arriveAirports;
	String dayOver;

	public String getCarriers() {
		return carriers;
	}

	public void setCarriers() {
		if(this.departAirports.get(0).getAirline().getName()!=null){
			for (int i = 0; i < this.departAirports.size(); i++) {
				carriers += " - "
						+ this.departAirports.get(i).getAirline().getName();
			}
			this.carriers = carriers.substring(3);
		}
	}

	public String getAirportsExpanded() {
		return airportsExpanded;
	}

	public void setAirportsExpanded(String airportsExpanded) {
		this.airportsExpanded = airportsExpanded;
	}

	public String getNumberStops() {
		return numberStops;
	}

	public void setNumberStops(String numberStops) {
		this.numberStops = numberStops;
	}

	/**
	 * @return Ex: 18h30
	 */
	public String getTotalDurationHHMM() {
		return totalDurationHHMM;
	}

	public void setTotalDurationHHMM() {
		int duration = Integer.parseInt(totalDuration);
		if (duration > 60) {
			int hours = duration / 60;
			int mins = duration - (hours * 60);
			String hoursStr = "";
			String minsStr = "";
			if (hours < 10) {
				hoursStr = "0" + String.valueOf(hours);
			} else {
				hoursStr = String.valueOf(hours);
			}
			if (mins < 10) {
				minsStr = "0" + String.valueOf(mins);
			} else {
				minsStr = String.valueOf(mins);
			}
			totalDurationHHMM = hoursStr + "h" + minsStr + "m";
		} else {
			totalDurationHHMM = String.valueOf(duration) + "m";
		}
	}

	public String getTotalDuration() {
		return totalDuration;
	}

	public void setTotalDuration(String totalDuration) {
		this.totalDuration = totalDuration;
		setTotalDurationHHMM();
	}

	public List<AirportDeparture> getDepartAirports() {
		return departAirports;
	}

	public void setDepartAirports(List<AirportDeparture> departAirports) {
		this.departAirports = departAirports;
		setCarriers();
	}

	public List<AirportArrival> getArriveAirports() {
		return arriveAirports;
	}

	public void setArriveAirports(List<AirportArrival> arriveAirports) {
		this.arriveAirports = arriveAirports;
	}

	public String getDayOver() {
		return dayOver;
	}

	public void setDayOver() {
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
			Date departureDate = sdf.parse(this.getDepartAirports().get(0)
					.getDepartureTimeYYYYMMDD());
			Date arrivalDate = sdf.parse(this.getArriveAirports()
					.get(this.getArriveAirports().size() - 1)
					.getArrivalTimeYYYYMMDD());
			this.dayOver = Utils.getDaysBetweenTwoDate(departureDate,
					arrivalDate) + "";
		} catch (ParseException ex) {
			Logger.getLogger(OutBound.class).info("setDayOver parse exception",
					ex);
		}
	}

}
